using System;
using System.Collections.Generic;
using System.Text;

namespace SalesMain
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] groups = new int[m_NumGroups][];
            
            CreateGroups(groups);

            SetMonthSales(groups);

            int[][] salaries = new int[m_NumGroups][];
            int[] best = new int[3];

            CalcSalaries(groups, salaries, best);

            Report(salaries, best);

            return;
        }

        static void CreateGroups(int[][] groups)
        {
            Random rnd = new Random();

            for (int i = 0; i < m_NumGroups; i++)
            {
                groups[i] = new int[rnd.Next(10, 21)];
            }
        }

        static void SetMonthSales(int[][] groups)
        {
            Random rnd = new Random();

            for (int i = 0; i < m_NumGroups; i++)
            {
                for (int j = 0; j < groups[i].Length; j++)
                {
                    groups[i][j] = rnd.Next(0, 100001);
                }
            }
        }

        //
        // best[0] : group #of the best sales person
        // best[1] : the best sales person number in the group
        // best[2] : the best group number
        static void CalcSalaries(int[][] groups, int[][] salaries, int[] best)
        {
            int bestPersonGrpNum = -1;
            int bestPersonNum = -1;
            int bestGrpNum = -1;
            long maxPersonSales = -1;
            long maxGrpAvgSales = -1;

            for (int i = 0; i < m_NumGroups; i++)
            {
                salaries[i] = new int[groups[i].Length];
                long grpSales = 0;

                for (int j = 0; j < groups[i].Length; j++)
                {
                    salaries[i][j] = 2000 + groups[i][j] / 10;

                    if (groups[i][j] > maxPersonSales)
                    {
                        maxPersonSales = groups[i][j];
                        bestPersonGrpNum = i;
                        bestPersonNum = j;
                    }

                    grpSales += groups[i][j];
                }

                long grpAvgSales = grpSales / groups[i].Length;

                if (grpAvgSales > maxGrpAvgSales)
                {
                    maxGrpAvgSales = grpAvgSales;
                    bestGrpNum = i;
                }
            }

            salaries[bestPersonGrpNum][bestPersonNum] += 1000;

            for (int j = 0; j < groups[bestGrpNum].Length; j++)
            {
                salaries[bestGrpNum][j] += 500;
            }

            best[0] = bestPersonGrpNum;
            best[1] = bestPersonNum;
            best[2] = bestGrpNum;
        }

        static void Report(int[][] salaries, int[] best)
        {
            Console.WriteLine("==== Salaries: ====");

            for (int i = 0; i < m_NumGroups; i++)
            {
                Console.WriteLine("Group #" + i.ToString());

                for (int j = 0; j < salaries[i].Length; j++)
                {
                    Console.Write(salaries[i][j].ToString() + " ");
                }

                Console.WriteLine();
            }

            Console.WriteLine(
                "The best worker salary (group " +
                best[0].ToString() + ", number " +
                best[1].ToString() + "): " + salaries[best[0]][best[1]]);

            Console.WriteLine("The best group number: " + best[2].ToString());

        }

        const int m_NumGroups = 5;
    }
}
